package cn.tedu.audit.service.impl;


import cn.tedu.application.mapper.ApplicationMapper;
import cn.tedu.application.pojo.entity.Application;
import cn.tedu.audit.base.enums.AuditStatusEnums;
import cn.tedu.audit.mapper.AuditMapper;
import cn.tedu.audit.pojo.entity.Audit;
import cn.tedu.audit.pojo.vo.AuditVO;
import cn.tedu.audit.service.AuditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.util.Collections;
import java.util.List;

@Service
@Slf4j
@Transactional
public class AuditServiceImpl  implements AuditService {

    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditMapper auditMapper;

    @Override
    public void insertAudit(Application application) {
        log.debug("为申请单生成对应的审批单业务:application={}",application);
        //获取审批人id集合
        List<Long> userIdList = application.getAuditUserIdList();
        //遍历审批人集合，根据申请单数据生成审批数据
        for (int i = 0; i < userIdList.size(); i++){
            //设置审批单数据
            Audit audit = new Audit();
            audit.setApplicationId(application.getId());//设置申请单id
            audit.setAuditUserId(userIdList.get(i));//设置审批人id
            audit.setAuditSort(i);//设置审批次序，0为直属领导，数字越大等级越高
            if (i==0){  //0表示该条审批数据为待我审核
                audit.setAuditStatus(AuditStatusEnums.MY_PENDING.getCode());
            }else { //除了0之外的数字的审批数据都为待他人审核
                audit.setAuditStatus(AuditStatusEnums.PENDING.getCode()
                );
            }
            //设置创建时间
            audit.setCreateTime(application.getCreateTime());
            //每循环一次，向数据库插入一条数据
            auditMapper.insert(audit);
        }
    }

    @Override
    public List<AuditVO> selectAuditByApplicationId(Long id) {
        return auditMapper.selectAuditByApplicationId(id);
    }
}
